package com.ezops.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ezops.dao.TradeClientDAO;
import com.ezops.dao.facade.IFileMonitorFacade;
import com.ezops.dto.FileData;
import com.ezops.util.Notification;
import com.ezops.util.NotificationSender;

/**
 * Controller class to handle all requests related to File monitoring.
 * 
 * @author vgaurav
 * 
 */
@Controller
@RequestMapping("/filemonitor")
public class FileMoniorController {

	@Autowired
	private IFileMonitorFacade facade;

	@Autowired
	private NotificationSender sender;

	@Autowired
	private TradeClientDAO clientDAO;

	@RequestMapping(value = "/getfiledata", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody
	List<FileData> getFileData() {
		return facade.getAllFileData();
	}

	@RequestMapping(value = "/getTradeStatus", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody
	Map<String, String> getTradeStatus() {

		return facade.getTradeStatus();
	}

	@RequestMapping(value = "/notify", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody
	Map<String, String> sendMail(
			@RequestParam(value = "selectedBatchId", required = true) String batchId,
			@RequestParam("notificationMessage") String message)
			throws Exception {
		Map<String, String> output = new HashMap<String, String>();
		final String batchIdTem = batchId;
		final String messageToSend = message;

		int clientId = facade.getClientForBatch(Integer.parseInt(batchId));
		final String clientMailId = clientDAO.find(clientId).getClientEmail();

		sender.send(new Notification() {
			String message = "<html> <body><b>Trade Failed !! </b> <p> Trade file failed for  batch Id "
					+ batchIdTem.toString()
					+ " . Message from operations - "
					+ messageToSend.toString() + "</body></html>";

			@Override
			public List<String> getReceipeints() {
				return Arrays.asList(clientMailId);
			}

			@Override
			public String getMessage() {
				// TODO Auto-generated method stub
				return message;
			}
		});

		output.put("message", "Noification sent successfully");
		return output;
	}

}
